#include "changerecordform.h"
#include "appmanager.h"
#include "ui_recordform.h"


ChangeRecordForm::ChangeRecordForm(QWidget *parent) :
    RecordForm(parent)
{
    setWindowTitle("Редагувати обраний запис");
}

void ChangeRecordForm::setup(const int id, QString name, const int styleId, QString comment)
{
    qDebug()<<"IN ChangeRecordForm::setup";
    if (appManager->stylesLoaded() == AllStylesLoaded)
    {
        ui->cbStyle->clear();
        foreach(Style st, appManager->mStyles)
        {
            ui->cbStyle->addItem(QIcon(st.img), QString("%1. %2").arg(st.uniqId).arg(st.name));
        }
    }

    ui->edtUniqId->setText(QString("%1").arg(id));
    ui->edtName->setText(name);
    ui->edtComment->setText(comment);

    ui->cbStyle->setCurrentIndex(styleId);
}

void ChangeRecordForm::on_btnCancel_clicked()
{
    this->close();
}

void ChangeRecordForm::on_btnAccept_clicked()
{
    qDebug()<<"IN ChangeRecordForm::on_btnAccept_clicked()";
    QString styleId(ui->cbStyle->currentText());
    styleId = styleId.left(styleId.indexOf("."));

    bool res = false;
    res = appManager->dbWorker()->editCurRecord(
                ui->edtUniqId->text().toInt(),
                ui->edtName->text(),
                styleId.toInt(),
                ui->edtComment->text());

    if (res)
    {
        appManager->dbWorker()->refreshAllModels();
        appManager->mainWindow()->refreshViews();
        LOG ("ChangeRecordForm::on_btnAccept_clicked", "Зміни успішно внесені", "" );
    }
    else
    {
        LOG ("ChangeRecordForm::on_btnAccept_clicked", "Проблема при внесенні змін!", "" );
    }
    this->close();
}
